import pymysql


# 插入数据
def addProblem(data):
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", passwd="123456", db="Generated_problems")

    # 使用cursor()方法获取操作游标
    cursor = db.cursor()

    sql = "INSERT INTO mytable(problem) VALUES (%s)"
    params = (data,)
    cursor.execute(sql, params)
    db.commit()

    # 关闭数据库连接
    db.close()

def getProblem(id):
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", passwd="123456", db="Generated_problems")

    # 使用cursor()方法获取操作游标
    cursor = db.cursor()

    # 查询数据
    sql = "SELECT * FROM mytable WHERE id = %s"
    params = (18,)
    cursor.execute(sql, params)
    results = cursor.fetchall()

    # 打印查询结果
    res = ""
    for row in results:
        res = row[1]


    # 关闭数据库连接
    db.close()
    return res
def getAllproblems():
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", passwd="123456", db="Generated_problems")

    # 使用cursor()方法获取操作游标
    cursor = db.cursor()

    # 查询数据
    sql = "SELECT * FROM mytable"
    cursor.execute(sql)
    results = cursor.fetchall()

    # 打印查询结果

    # 关闭数据库连接
    db.close()
    return results
def addComplementaryProblem(tag, content,solution,testCase):
    # 打开数据库连接
    db = pymysql.connect(host="localhost", user="root", passwd="123456", db="Generated_problems")

    # 使用cursor()方法获取操作游标
    cursor = db.cursor()

    sql = "INSERT INTO complementary(tag,problem,solution,textCase) VALUES (%s,%s,%s,%s)"
    params = (tag, content,solution,testCase)
    cursor.execute(sql, params)
    db.commit()

    # 关闭数据库连接
    db.close()
getProblem(3)


